package com.tea.admin.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tea.common.entity.app.dto.AdminGoodsDTO;
import com.tea.common.entity.app.dto.GoodsDTO;
import com.tea.admin.modules.app.mapper.GoodsAdminMapper;
import com.tea.admin.modules.app.service.GoodsAdminService;
import com.tea.common.common.annotation.NeedPermission;
import com.tea.common.common.exception.ServiceException;
import com.tea.common.common.util.result.Result;
import com.tea.common.entity.GoodsStore;
import com.tea.common.mapper.GoodsStoreMapper;
import com.tea.common.service.GoodsStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "系统：商铺管理")
@RestController
@RequestMapping("/goodsAdmin")
public class GoodsAdminController {

    @Resource
    private GoodsAdminService goodsAdminService;
    @Resource
    private GoodsAdminMapper goodsAdminMapper;
    @Resource
    private GoodsStoreMapper goodsStoreMapper;


    @ApiOperation("分页查询")
    @NeedPermission("system:app:goods:list")
    @GetMapping("/page")
    public Result<Page<AdminGoodsDTO>> getGoodsAdminByPage(@RequestParam(defaultValue = "1") int pageNo,
                                                           @RequestParam(defaultValue = "10") int pageSize,
                                                           @RequestParam(required = false) Long storeId) {
        return Result.ok(goodsAdminService.getGoodsAdminByPage(pageNo, pageSize, storeId));
    }

    @ApiOperation("查询一个商品")
    @NeedPermission("system:app:goods:list")
    @GetMapping("/{goodsId}")
    public Result<GoodsDTO> getGoodsById(@PathVariable Integer goodsId) {
        return Result.ok(goodsAdminService.getGoodsById(goodsId));
    }

    @ApiOperation("增加")
    @NeedPermission("system:app:goods:add")
    @PostMapping("")
    public Result<Integer> add(@RequestBody GoodsDTO goodsAdmin) {
        return Result.ok(goodsAdminService.addGoodsAdmin(goodsAdmin));
    }

    @ApiOperation("批量删除")
    @NeedPermission("system:app:goods:delete")
    @DeleteMapping("/batch")
    public Result<Integer> delete(@RequestBody List<Integer> idList) {
        return Result.ok(goodsAdminService.deleteGoodsAdminBatchIds(idList));
    }

    @ApiOperation("修改")
    @NeedPermission("system:app:goods:update")
    @PutMapping("")
    public Result<Integer> update(@RequestBody GoodsDTO goodsAdmin) {
        return Result.ok(goodsAdminService.updateGoodsAdmin(goodsAdmin));
    }

    @ApiOperation("下架或上架商品")
    @NeedPermission("system:app:goods:update")
    @GetMapping("/updateSellStatus")
    public Result<Integer> updateSellStatus(@RequestParam("goodsId") Integer goodsId, @RequestParam("storeId") Integer storeId) {

        return Result.ok(goodsStoreMapper.update(null, new LambdaUpdateWrapper<GoodsStore>()
                .eq(GoodsStore::getGoodsId, goodsId)
                .eq(GoodsStore::getStoreId, storeId)
                .set(GoodsStore::getStatus, goodsStoreMapper.selectOne(new LambdaQueryWrapper<GoodsStore>()
                        .eq(GoodsStore::getGoodsId,goodsId)
                        .eq(GoodsStore::getStoreId,storeId)).getStatus() == 1 ? 0 : 1)));
    }

    @ApiOperation("更新商品的图片")
    @NeedPermission("system:app:goods:update")
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    public Result<Integer> updateGoodsImage(
            @ApiParam(value = "商品ID", required = true) @RequestParam Integer goodsId, 
            @ApiParam(value = "图片文件", required = true) @RequestParam MultipartFile file) throws ServiceException {
        if (file == null || file.isEmpty()) {
            return Result.fail("请选择要上传的图片");
        }
        return Result.ok(goodsAdminService.updateGoodsImage(goodsId, file));
    }

    @ApiOperation("下架或上架商品")
    @NeedPermission("system:app:goods:update")
    @PutMapping("/updateRecommendStatus/{goodsId}")
    public Result<Integer> updateRecommendStatus(@PathVariable Integer goodsId) {
        return Result.ok(goodsAdminMapper.updateRecommendStatus(goodsId));
    }
    
    @ApiOperation("获取商品关联的门店")
    @NeedPermission("system:app:goods:list")
    @GetMapping("/{goodsId}/stores")
    public Result<List<GoodsStore>> getGoodsStores(@PathVariable Integer goodsId) {
        return Result.ok(goodsAdminService.getGoodsStores(goodsId));
    }


}
